package com.zane.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 统一返回结果类
 * @param <T>
 */

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> implements Serializable {
    private Integer code;
    private String message;
    private T data;

    public static <T> Result<T> success(String msg){
        return new Result<>(200,msg,null);
    }

    public static<T> Result<T> success(T data){
        return new Result<>(200,"操作成功",data);
    }

    public static<T> Result<T> success(String msg,T data){
        return new Result<>(200,msg,data);
    }

    public static<T> Result<T> error(String msg){
        return new Result<>(500,msg,null);
    }

    /**
     * 自定义错误码，无业务数据
     * @param code
     * @param msg
     * @return
     * @param <T>
     */
    public static<T> Result<T> error(Integer code,String msg){
        return new Result<>(code,msg,null);
    }

    /**
     * 自定义错误码，返回错误详情
     * @param code
     * @param msg
     * @param data
     * @return
     * @param <T>
     */
    public static<T> Result<T> error(Integer code,String msg,T data){
        return new Result<>(code,msg,data);
    }
}
